/**
 * 地址列表
 * */
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import { Toast } from 'antd-mobile'
import common from '../../utils/common'
import * as addressActions from '../../actions/addressActions'
import { AddrList } from 'app/components'

class AddressList extends Component {
    constructor(props) {
        super(props)
        this.state = {
            isLoading: true,
        }
    }

	/**
	 * 生命周期
	 */
    componentWillMount() {
        common.setViewTitle('选择收件地址');
    }
    componentWillUnmount() {
        // sessionStorage.setItem('getRegionType', 1)
        // sessionStorage.setItem('focusAddressList', 1)
        // console.log('缓存的地址',this.props.addressList[this.state.type])
    }


    componentDidMount() {
        this.getAddressList();
    }

    //获得地址列表
    getAddressList() {
        this.props.addressActions.getAddressListAsync({}, (result) => {
            console.log(result);
            this.setState({ isLoading: false })
        })
    }

    //选择的默认地址
    getDataFUN(type, Id, opt) {
        //opt default 设置默认地址
        // choice  选择的地址

        // if (opt == 'choice') {
        //     sessionStorage.setItem('choiceAddress', JSON.stringify(Object.assign({}, this.state.addressList[type])))  //选择的地址
        //     this.props.dispatch(showChiceAddress(false))
        //     sessionStorage.setItem('addressListGetIntId', 1)
        //     // history.go(-1);
        //     this.toUrl('home')
        //     return
        // } else {
        //     let postData = {  //设置默认地址接口
        //         id: Id
        //     }
        //     this.props.dispatch(setDefaultAddress(postData, (res) => { }));

        //     this.props.dispatch(showChiceAddress(false))
        //     sessionStorage.setItem('address', JSON.stringify(Object.assign({}, this.state.addressList[type])));  //默认地址
        //     // localStorage.setItem("addressList_action", type); //存储列表勾选的位置
        //     this.setState({
        //         type: type,
        //     });

        // }
    }

    // 添加地址
    addAddr(){
        this.props.router.push("addAddress");
    }

    // 编辑地址
    editAddr(index){
        let { addressList } = this.props;
        let id = addressList[index].id;
        if(id)this.props.router.push(`addressEdit/${id}`);
    }

    // 删除地址
    deleteAddr(index){
        let { addressList } = this.props;
        let id = addressList[index].id;
    }

    render() {
        let { isLoading } = this.state;
        let { addressList } = this.props
        return (
            <AddrList
                isLoading={isLoading}
                defaultIndex={0}
                addressList={addressList}
                handleAdd={() => this.addAddr()}
                handleDelte={(index) => this.deleteAddr(index)}
                handleEdit={(index) => this.editAddr(index)}
                handleSelect={(index) => console.log("选择地址", index)}
                setDefault={(index) => console.log("设置为默认值", index)}
                />
        )
    }
}


const mapStateToProps = state => ({
    addressList: state.addressList,
})

const mapDispatchToProps = dispatch => ({
    addressActions: bindActionCreators(addressActions, dispatch),
})

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(AddressList)